#include "Book.h"
#include <sstream>

Book::Book() : totalCount(0), availableCount(0), status(true) {}

Book::Book(string id, string t, string a, string p, string c, int total)
    : bookId(id), title(t), author(a), publisher(p), category(c),
    totalCount(total), availableCount(total), status(true) {
}

void Book::displayInfo() const {
    cout << "书号: " << bookId << endl;
    cout << "书名: " << title << endl;
    cout << "作者: " << author << endl;
    cout << "出版社: " << publisher << endl;
    cout << "分类: " << category << endl;
    cout << "总藏书量: " << totalCount << endl;
    cout << "可借数量: " << availableCount << endl;
    cout << "状态: " << (status ? "可借" : "不可借") << endl;
    cout << "----------------------------------------" << endl;
}

string Book::toString() const {
    stringstream ss;
    ss << bookId << "|" << title << "|" << author << "|"
        << publisher << "|" << category << "|" << totalCount << "|"
        << availableCount << "|" << status;
    return ss.str();
}

Book Book::fromString(const string& data) {
    stringstream ss(data);
    string bookId, title, author, publisher, category;
    int totalCount, availableCount;
    bool status;

    getline(ss, bookId, '|');
    getline(ss, title, '|');
    getline(ss, author, '|');
    getline(ss, publisher, '|');
    getline(ss, category, '|');
    ss >> totalCount; ss.ignore();
    ss >> availableCount; ss.ignore();
    ss >> status;

    Book book(bookId, title, author, publisher, category, totalCount);
    book.setAvailableCount(availableCount);
    book.setStatus(status);
    return book;
}